var $ = layui.$;


var nextVedio = {
	tabId:null,
	urlmatch:null,
	init:function(){
		if(chrome.tabs.getCurrent){
			chrome.tabs.query({active: true, lastFocusedWindow: true},function (tab) {
				var url = tab[0].url;
				nextVedio.tabId = tab[0].id;
//				nextVedio.notice(3,[1]);
				$("#urlTxt").html(url);
				var yuming =  chromeUtils.getYuming(url);
				if(!yuming)return;
				nextVedio.urlmatch = yuming;
				$("#urlmatch").val(nextVedio.urlmatch);
				chrome.storage.sync.get(nextVedio.urlmatch, function(res) {
					res = res[nextVedio.urlmatch];
					
					if(res){
						if(res.switchOn){
							$("[name=switchOn]").attr("checked",true);
						}
						layui.form.render();
					}
				});
				
			});
			nextVedio.setListener();
		}else if(chrome.tabs.getSelected){
			chrome.tabs.getSelected(null, function (tab) {
				var url = tab.url;
				nextVedio.tabId = tab.id;
//				nextVedio.notice(3,[1]);
				$("#urlTxt").html(url);
				var yuming = chromeUtils.getYuming(url);
				if(!yuming)return;
				nextVedio.urlmatch = yuming;
				chrome.storage.sync.get(nextVedio.urlmatch, function(res) {
					res = res[nextVedio.urlmatch];
					$("#urlmatch").val(nextVedio.urlmatch);
					if(res){
						$("#urlmatch").val(nextVedio.urlmatch || yuming);
						if(res.switch){
							$("[name=name]").attr("checked",true);
						}
						layui.form.render();
					}
				});
				$("#urlTxt").html(tab.url);
			});
			nextVedio.setListener();
		}else if(chrome.tabs.query){
			chrome.tabs.query({active: true, lastFocusedWindow: true}, 
				
				tabs => {
			    var url = tabs[0].url;
			    // use `url` here inside the callback because it's asynchronous!
			});
		}else{
			layui.layer.msg("无api可用");
		}
		
	},
	setListener:function(){
		$("#save").click(function(e){
			layui.layer.msg("保存成功");
			var tagName = $("[name=tagName]").val() || "";
			var idName = $("[name=idName]").val()|| "";
			var className = $("[name=className]").val()|| "";
			var name = $("[name=name]").val()|| "";
			var urlmatch = $("#urlmatch").val();
			var switchOn = $("[name=switchOn]").is(":checked")|| ""; 
			var obj = {tagName:tagName,idName:idName,className:className,name:name,switchOn:switchOn};
			var objtosave = {};
			objtosave[nextVedio.urlmatch] = obj;
			chrome.storage.sync.set(objtosave, function(res) {
				
			});
		});
		layui.form.on("switch(switchOn)", function(data){
		    if(this.checked){
		    	nextVedio.notice(1);
		    }else{
		    	nextVedio.notice(2);
		    }
		});
		
	},
	notice:function(type,args){
		
		function removeNextBtn() {
		    nexthtml.removeNextBtn();
		}
		function showNextBtn() {
		    nexthtml.showNextBtn();
		}
		function toNext(i) {
		    nexthtml.toNext(i);
		    
		}
		if(type == 1){
			chrome.scripting.executeScript({
			    target: { tabId: nextVedio.tabId },
			    function: showNextBtn,
			});
		}else if(type == 2){
			chrome.scripting.executeScript({
			    target: { tabId: nextVedio.tabId },
			    function: removeNextBtn,
			});
		}else if(type == 3){
			chrome.scripting.executeScript({
			    target: { tabId: nextVedio.tabId },
			    function: toNext,
			    args:args
			});
			window.close();
			setTimeout(function(){
				chrome.tabs.remove(nextVedio.tabId);
			},200);
//		    chrome.tabs.remove(nextVedio.tabId);
		}
	}
}
layui.use(['jquery','form','layer'],function(){
	$ = layui.$;
	nextVedio.init();
});